﻿
using Chain.BLL;
using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class historyconvertgiftByStoresDetail : Page
{

    private int TotalSubtotal;

    private void DataBindGiftCard(HttpCookie cookie)
    {
        PointGift PointGiftBll = new PointGift();
        StringBuilder sbWhere = new StringBuilder("GiftID in(");
        string[] keys = HttpUtility.UrlDecode(cookie.Value).Trim().Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string item in keys)
        {
            int GiftID = Convert.ToInt32(item.Split(new char[] { ',' })[0]);
            Convert.ToInt32(item.Split(new char[] { ',' })[1]);
            sbWhere.AppendFormat("{0},", GiftID);
        }
        sbWhere.Append("0)");
        DataTable dt = PointGiftBll.GetList(sbWhere.ToString()).Tables[0];
        dt = this.UpDateTable(dt, keys);
        this.rptHistoryconvertgiftByStoresDetail.DataSource = dt;
        this.rptHistoryconvertgiftByStoresDetail.DataBind();
    }

    private string GetGiftlist(int ExchangeID)
    {
        StringBuilder sbGiftlist = new StringBuilder();
        DataTable dt = new GiftExchangeDetail().GetGiftExchangeDetailByExchangeID(ExchangeID);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sbGiftlist.AppendFormat("{0},{1}|", dt.Rows[i]["ExchangeGiftID"], dt.Rows[i]["ExchangeNumber"]);
        }
        return sbGiftlist.ToString();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie cookie = new HttpCookie("giftlist", this.Request["giftlist"]);
      //  HttpCookie cookie = base.Request.Cookies["giftlist"];
        string type = base.Request["type"];
        if (!string.IsNullOrEmpty(type) && (type == "lookinventory"))
        {
            cookie.Value = this.GetGiftlist(Convert.ToInt32(base.Request["giftlist"]));
        }
        if ((cookie != null) && !string.IsNullOrEmpty(cookie.Value))
        {
            this.DataBindGiftCard(cookie);
            this.totalPoint.InnerText = this.TotalSubtotal.ToString();
        }
    }

    private DataTable UpDateTable(DataTable dt, string[] keys)
    {
        dt.Columns.Add("ConvertNumber");
        dt.Columns.Add("Subtotal");
        foreach (string item in keys)
        {
            int GiftID = Convert.ToInt32(item.Split(new char[] { ',' })[0]);
            int ConvertNumber = Convert.ToInt32(item.Split(new char[] { ',' })[1]);
            DataRow row = dt.Select("GiftID=" + GiftID)[0];
            row["ConvertNumber"] = ConvertNumber;
            row["Subtotal"] = ConvertNumber * Convert.ToInt32(row["GiftExchangePoint"]);
            this.TotalSubtotal += Convert.ToInt32(row["Subtotal"]);
        }
        return dt;
    }
}


